using System;
using System.IO;
using System.Collections;
using System.Linq;
using System.Reflection;
using UnityEngine;
using System.Collections.Generic;

public class HybridCLRLoader : MonoBehaviour
{
    IEnumerator Start()
    {
        AssetBundleManager abmgr = AssetBundleManager.Instance;
        abmgr.Init();
        yield return TestInit();
        // Editor�����£�HotUpdate.dll.bytes�Ѿ����Զ����أ�����Ҫ���أ��ظ����ط���������⡣
#if !UNITY_EDITOR
        Assembly hotUpdateAss = Assembly.Load(File.ReadAllBytes($"{Path.Combine(Application.persistentDataPath,abmgr.SCRIPT_FOLDERNAME)}/HotUpdate.dll.bytes"));
#else
        // Editor��������أ�ֱ�Ӳ��һ��HotUpdate����
        Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "HotUpdate");
#endif
        Type type = hotUpdateAss.GetType("Hello");
        type.GetMethod("Run").Invoke(null, null);
    }

    List<string> filesTable;
    List<string> bundlTable;
    public IEnumerator TestInit()
    {
        ResourceLoadPanel rlp = UIManager.Instance.InitPlane<ResourceLoadPanel>(0, CanvasType.Front, null, true);
        rlp.Open(Vector3.zero, Vector3.one);

        yield return new WaitForSeconds(1);

        AssetBundleManager abmgr = AssetBundleManager.Instance;
        filesTable = new List<string>();
        bundlTable = new List<string>();
        //�����ȸ��½ű�
        yield return abmgr.DownloadScripts("HotUpdate.dll.bytes");
        //�������ñ�
        //yield return StartCoroutine(ReadTabelFileHead('|', Path.Combine(Application.streamingAssetsPath, CONFIG_FILEHEAD), filesTable));
        rlp.SetPorcess(25);
        yield return new WaitForSeconds(1);
        filesTable.Add("TestConfigTable.txt");
        yield return abmgr.DownloadTables(filesTable.ToArray());
        //����AB��
        rlp.SetPorcess(50);
        yield return new WaitForSeconds(1);

        bundlTable.Add(abmgr.UI_BUNDLE);
        bundlTable.Add(abmgr.SOUND_BUNDLE);
        //bundlTable.Add(abmgr.POOL_BUNDLE);
        bundlTable.Add(abmgr.SCENE_BUNDLE);
        bundlTable.Add("prefab_arpg");
        bundlTable.Add("prefab_common");
        bundlTable.Add("prefab_uinode");
        //yield return StartCoroutine(ReadTabelFileHead(',', Path.Combine(Application.streamingAssetsPath, BUNDLS_FILEHEAD), bundlTable));
        yield return abmgr.DownloadBundles(bundlTable.ToArray());
        UIManager.Instance.SetUIBundle(abmgr.bundles[abmgr.UI_BUNDLE]);
      
        rlp.SetPorcess(100);
        yield return new WaitForSeconds(1);
        rlp.Close();
    }


}
